package team.tunan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import team.tunan.common.HttpCodeEnum;
import team.tunan.dto.EmployeePageDto;
import team.tunan.entity.Employee;
import team.tunan.exception.BusinessException;
import team.tunan.mapper.EmployeeMapper;
import team.tunan.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import static team.tunan.utils.BaseRegexPatterns.PASSWORD_REGEX;
import static team.tunan.utils.BaseRegexPatterns.PHONE_REGEX;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author sqx
 * @since 2023-12-24
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public int insertEmployee(Employee employee) {
       // checkEmployee(employee);
        return employeeMapper.insert(employee);
    }

    @Override
    public int updateEmployee(Employee employee) {
        //checkEmployee(employee);
        return employeeMapper.updateById(employee);
    }

    @Override
    public int deleteEmployee(String id) {
        return employeeMapper.deleteById(id);
    }

    @Override
    public Page<Employee> selectPage(EmployeePageDto employeePageDto) {
        Page<Employee> employeePage = new Page<>(employeePageDto.getCurrentPage(), employeePageDto.getPageSize());
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
        if(employeePageDto.getUserName()!=null&& !employeePageDto.getUserName().equals("")){
            queryWrapper.like(Employee::getUserName,employeePageDto.getUserName());
        }
        if(employeePageDto.getRole()!=null){
            queryWrapper.eq(Employee::getRole,employeePageDto.getRole());
        }
        Page<Employee> page = employeeMapper.selectPage(employeePage, queryWrapper);
        return page;
    }

    @Override
    public Boolean findByUserName(String userName) {
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUserName, userName);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        if (employee!=null){
            return true;
        }
        return false;
    }

    @Override
    public Employee login(Employee employee) {
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUserName, employee.getUserName()).eq(Employee::getPassword, employee.getPassword());
        return employeeMapper.selectOne(queryWrapper);
    }

    @Override
    public Employee selectByEmployeeId(Integer id) {
        return employeeMapper.selectById(id);
    }


    /**
     * 校验员工信息是否合法
     * @param employee
     */
    private void checkEmployee(Employee employee) {
        if(employee.getPhone().matches(PHONE_REGEX)) {
            throw new BusinessException(HttpCodeEnum.OPERATION_ERROR.getCode(), "手机号格式不正确,请输入正确的手机号");
        }
        if(employee.getPassword().matches(PASSWORD_REGEX)) {
            throw new BusinessException(HttpCodeEnum.OPERATION_ERROR.getCode(), "密码输入错误，密码应该是4-32位的字母数字下划线的组合");
        }
        if(employee.getAge() <= 18 || employee.getAge() >= 70) {
            throw new BusinessException(HttpCodeEnum.OPERATION_ERROR.getCode(), "年龄输入错误，年龄应该在18-70之间");
        }
    }

}
